<?php

/**
 * This is the model class for table "user_vote".
 *
 * The followings are the available columns in table 'user_vote':
 * @property integer $user_id
 * @property integer $user_voted
 * @property string $date_voted
 * @property integer $has_watched_vote
 * @property integer $vote
 * @property integer $mutual
 */
class mUserVote extends CActiveRecord {

    /**
     * Returns the static model of the specified AR class.
     * @param string $className active record class name.
     * @return mUserVote the static model class
     */
    public static function model($className = __CLASS__) {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName() {
        return 'user_vote';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
          array('user_id, user_voted, date_voted, has_watched_vote, vote, mutual', 'required'),
          array('user_id, user_voted, has_watched_vote, vote, mutual', 'numerical', 'integerOnly' => true),
          // The following rule is used by search().
          // Please remove those attributes that should not be searched.
          array('user_id, user_voted, date_voted, has_watched_vote, vote, mutual', 'safe', 'on' => 'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations() {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
        return array(
          'user_id' => 'User',
          'user_voted' => 'User Voted',
          'date_voted' => 'Date Voted',
          'has_watched_vote' => 'Has Watched Vote',
          'vote' => 'Vote',
          'mutual' => 'Mutual',
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search() {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria = new CDbCriteria;

        $criteria->compare('user_id', $this->user_id);
        $criteria->compare('user_voted', $this->user_voted);
        $criteria->compare('date_voted', $this->date_voted, true);
        $criteria->compare('has_watched_vote', $this->has_watched_vote);
        $criteria->compare('vote', $this->vote);
        $criteria->compare('mutual', $this->mutual);

        return new CActiveDataProvider($this, array(
          'criteria' => $criteria,
        ));
    }

    public function getUserVote($user_id, $limit) {
        $user = Yii::app()->db->createCommand()
            ->select('p.*, uv.date_voted as date_view, u.username, up.link_img, ct.name as country_name, uf.user_favorited')
            ->from('user_vote uv')
            ->leftjoin('profiles p', 'p.user_id=uv.user_voted')
            ->leftjoin('users u', 'u.user_id=uv.user_voted')
            ->leftjoin('user_photos up', 'up.approve=1 and up.make_primary=1 and up.user_id=uv.user_voted')
            ->leftjoin('countries ct', 'ct.code = p.country')
            ->leftjoin('user_favorite uf', 'uf.user_favorited=uv.user_voted and uf.user_id=:user_id', array(':user_id' => $user_id))
            ->where('uv.user_id=:user_id', array(':user_id' => $user_id))
            ->limit($limit)
            ->order(array('RAND()'))
            ->queryAll();

        return $user;
    }

    public function getUserVoteWithPagination($user_id, $offset, $limit) {
        $user = Yii::app()->db->createCommand()
            ->select('p.*, uv.date_voted as date_view, u.username, up.link_img, ct.name as country_name, uf.user_favorited')
            ->from('user_vote uv')
            ->leftjoin('profiles p', 'p.user_id=uv.user_voted')
            ->leftjoin('users u', 'u.user_id=uv.user_voted')
            ->leftjoin('user_photos up', 'up.approve=1 and up.make_primary=1 and up.user_id=uv.user_voted')
            ->leftjoin('countries ct', 'ct.code = p.country')
            ->leftjoin('user_favorite uf', 'uf.user_favorited=uv.user_voted and uf.user_id=:user_id', array(':user_id' => $user_id))
            ->where('uv.user_id=:user_id', array(':user_id' => $user_id))
            ->limit($limit, $offset)
            ->queryAll();

        return $user;
    }

    public function getUserVotedWithPagination($user_voted, $offset, $limit) {
        $user = Yii::app()->db->createCommand()
            ->select('p.*, uv.date_voted as date_view, u.username, up.link_img, ct.name as country_name, uf.user_favorited')
            ->from('user_vote uv')
            ->leftjoin('profiles p', 'p.user_id=uv.user_id')
            ->leftjoin('users u', 'u.user_id=uv.user_id')
            ->leftjoin('user_photos up', 'up.approve=1 and up.make_primary=1 and up.user_id=uv.user_id')
            ->leftjoin('countries ct', 'ct.code = p.country')
            ->leftjoin('user_favorite uf', 'uf.user_favorited=uv.user_id and uf.user_id=:user_id', array(':user_id' => $user_voted))
            ->where('uv.user_voted=:user_voted and uv.mutual=0', array(':user_voted' => $user_voted))
            ->limit($limit, $offset)
            ->queryAll();
        return $user;
    }

    public function getUserVotedMutualWithPagination($user_voted, $offset, $limit) {
        $user = Yii::app()->db->createCommand()
            ->select('p.*, uv.date_voted as date_view, u.username, up.link_img, ct.name as country_name, uf.user_favorited')
            ->from('user_vote uv')
            ->leftjoin('profiles p', 'p.user_id=uv.user_id')
            ->leftjoin('users u', 'u.user_id=uv.user_id')
            ->leftjoin('user_photos up', 'up.approve=1 and up.make_primary=1 and up.user_id=uv.user_id')
            ->leftjoin('countries ct', 'ct.code = p.country')
            ->leftjoin('user_favorite uf', 'uf.user_favorited=uv.user_id and uf.user_id=:user_id', array(':user_id' => $user_voted))
            ->where('uv.user_voted=:user_voted and uv.mutual=1', array(':user_voted' => $user_voted))
            ->limit($limit, $offset)
            ->queryAll();
        return $user;
    }

}
